﻿using Bogus;
using Microsoft.EntityFrameworkCore;
using Model;
using Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class ProductService : IProductService
    {
        private IRepository<Product> _productRepository;
        public ProductService(IRepository<Product> productRepository)
        {
            _productRepository = productRepository;
        }

        /// <summary>
        /// 根据条件搜索
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="serach"></param>
        /// <returns></returns>
        public List<Product> GetAll(string sort = "", string serach = "")
        {
            var table = _productRepository.Table;
            switch (sort)
            {
                case "price_desc":
                    table = table.OrderByDescending(x => x.Price);
                    break;
                case "Time":
                    table = table.OrderBy(x => x.Createtime);
                    break;
                case "time_desc":
                    table = table.OrderByDescending(x => x.Createtime);
                    break;
                default:
                    table = table.OrderBy(x => x.Price);
                    break;
            }
            if (!string.IsNullOrEmpty(serach))
                table = table.Where(x => x.Name.Contains(serach));
            return table.ToList();
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product GetProductById(int id)
        {
            if (id == 0)
                return null;

            return _productRepository.GetById(id);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="prodect"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Insert(Product prodect)
        {
            if (prodect == null)
                throw new ArgumentNullException(nameof(prodect));

            _productRepository.Insert(prodect);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void CreateNewProduct()
        {
            var list = new List<Product>();
            var faker = new Faker(locale: "zh_CN");
            for (var i = 0; i < 30; i++)
            {
                list.Add(new Product
                {
                    Name = faker.Commerce.ProductName(),
                    Price = decimal.Parse(faker.Commerce.Price()),
                    Createtime = DateTime.Now
                });
            }
            _productRepository.Insert(list);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            var prodect = _productRepository.GetById(id);
            if (prodect == null)
                throw new ArgumentNullException(nameof(prodect));
            _productRepository.Delete(prodect);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="prodect"></param>
        public void Update(Product prodect)
        {
            if (prodect == null)
                throw new ArgumentNullException(nameof(prodect));

            _productRepository.Update(prodect);
        }
    }
}
